package com.dana.system.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.enums.ResultCode;
import com.dana.security.exception.ServiceException;
import com.dana.system.domain.user.User;
import com.dana.system.domain.user.dto.QueryUserDTO;
import com.dana.system.domain.user.dto.UpdateUserStatusDTO;
import com.dana.system.domain.user.vo.UserVO;
import com.dana.system.mapper.user.UserMapper;
import com.dana.system.service.user.IUserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements IUserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取用户列表
     * @param queryUserDTO
     * @return
     */
    @Override
    public List<UserVO> getList(QueryUserDTO queryUserDTO) {
        PageHelper.startPage(queryUserDTO.getPageNum(),queryUserDTO.getPageSize());
        return userMapper.selectUserList(queryUserDTO);
    }

    /**
     * 更新用户状态
     * @param updateUserStatusDTO
     * @return
     */
    @Override
    public int updateStatus(UpdateUserStatusDTO updateUserStatusDTO) {
        //根据id查询用户
        User user = userMapper.selectById(updateUserStatusDTO.getUserId());
        if (user==null){
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        user.setStatus(updateUserStatusDTO.getStatus());
        return userMapper.updateById(user);
    }
}
